package com.example.myservice_provider.controller;


import com.example.myservice_provider.entity.Response;
import com.example.myservice_provider.entity.Users;
import com.example.myservice_provider.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
public class Usercontroller {

    @Autowired
    private UserService userService;

    @PostMapping(value = {"/login"})
    public Response login(@RequestBody Users users) throws  Exception{
        Response response =new Response();

        System.out.println("login");
        Users login = userService.login(users.getUsername(), users.getPwd());
        response.setData(login);
        if(login==null){
            response.setHead("ERROR");
            response.setCode(0);
        }else {
            response.setHead("OK");
            response.setCode(1);
        }
        return response;
    }

    @PostMapping(value = {"/focus"})
    public Response focus(Integer befocusId,Integer focusId) throws  Exception{
        Response response =new Response();
        System.out.println(befocusId);
        response.setCode(this.userService.focusUser(focusId,befocusId));
        response.setHead("OK");
        return response;
    }

    @PostMapping(value = {"/cancelfocus"})
    public Response canclefocus(Integer befocusId,Integer focusId) throws  Exception{
        Response response =new Response();

        response.setCode(this.userService.cancelFocusUser(focusId,befocusId));
        response.setHead("OK");
        return response;
    }

    @PostMapping(value = {"/updateUserInfo"})
    public Response updateUserInfo(@RequestBody Users user) throws  Exception{
        Response response =new Response();
        int i = this.userService.updateUser(user);
        response.setCode(i);
        response.setHead("OK");
        return response;
    }


    @PostMapping(value = {"/getUserInfo"})
    public Response getUserInfo(Integer uid) throws  Exception{

        Response response = new Response();

        Users userInfo = this.userService.getUserInfo(uid);

        response.setHead("OK");
        if(userInfo!=null){
            response.setCode(1);
        }else{
            response.setCode(0);
        }
        response.setData(userInfo);
        return response;
    }
//    @PostMapping(value = {"/dept/{deptno}"})
//    public Users queryDept(@PathVariable Integer deptno, String name, Integer age) throws  Exception{
//        System.out.println("7070 provider");
//        System.out.println("name:"+name);
//        System.out.println("age:"+age);
//        //Thread.sleep(3000);//测试超时时间 人为延长时间测试
//        return null;
//    }
}
